jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }

        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
  function saveCitytoCookies(){
       var options = { expires: 10, path: '/' };
  		//清除过期的数据
       $.cookie('cfromcity', null);
       $.cookie('fromcity', null);
       $.cookie('ctocity', null);
       $.cookie('tocity', null);
       $.cookie('coutTicketCity', null);
       $.cookie('outTicketCity', null);

       $.cookie('cfromcity', $('#cfromCity').val(),options);
       $.cookie('fromcity', $('#fromCity').val(),options);
       $.cookie('ctocity', $('#ctoCity').val(),options);
       $.cookie('tocity', $('#toCity').val(),options);
       $.cookie('coutTicketCity', $('#coutTicketCity').val(),options);
       $.cookie('outTicketCity', $('#outTicketCity').val(),options);
       if($("#voyageTypevalue").val()==3){
	        $.cookie('cfromcity3', null);
	       	$.cookie('fromcity3', null);
	       	$.cookie('ctocity3', null);
	       	$.cookie('tocity3', null);

	        $.cookie('cfromcity3', $('#cfromCity3').val(),options);
	       	$.cookie('fromcity3', $('#fromCity3').val(),options);
	       	$.cookie('ctocity3', $('#ctoCity3').val(),options);
	       	$.cookie('tocity3', $('#toCity3').val(),options);
       }
  }
//城市提示
$(function () {
	
	$("#cfromCity").suggest(fcitys, {hot_list:fcommoncitys, dataContainer:"#fromCity", dataContainer2:"#outTicketCity", dataContainer3:"#coutTicketCity", attachObject:"#suggest0"});
	$("#cfromCity3").suggest(fcitys, {hot_list:fcommoncitys, dataContainer:"#fromCity3", attachObject:"#suggest2"});
	
	$("#ctoCity").suggest(fcitys, {hot_list:fcommoncitys, dataContainer:"#toCity", attachObject:"#suggest3"});
	$("#ctoCity3").suggest(fcitys, {hot_list:fcommoncitys, dataContainer:"#toCity3", attachObject:"#suggest4"});
	$("#coutTicketCity").suggest(outcitys, {hot_list:outcitys, dataContainer:"#outTicketCity", attachObject:"#suggest5"});
	if ($("#fromdate").val() == "") {
		$("#fromdate").val(TomorrowToStr());
	}

	if ($("#fromdate3").val() == "") {
		$("#fromdate3").val(nextDayToStr(TomorrowToStr()));
	}
	if ($("#todate").val() == "") {
		$("#todate").val(nextDayToStr(TomorrowToStr()));
	}
	$("#fromdate").attr("readonly", true);
	$("#fromdate3").attr("readonly", true);
	$("#todate").attr("readonly", true);
	//setCityDefaultValue();
});
function setCityDefaultValue(){
		var cfromcity = $.cookie('cfromcity');
		var fromcity = $.cookie('fromcity');

		var cfromcity_now = $("#cfromCity").val();


		if(cfromcity!= "" && cfromcity!=null&& cfromcity!="null"&&cfromcity_now=="中文/拼音" ){
				$("#cfromCity").val(cfromcity);
				$("#fromCity").val(fromcity);
		}else{
		    if ($("#cfromCity").val() == ""||$("#cfromCity").val() == "null"||$("#cfromCity").val() == null) {

				$("#cfromCity").val("\u5317\u4eac");
				$("#fromCity").val("PEK");
			}
		}

		var ctoCity = $.cookie('ctocity');
		var toCity = $.cookie('tocity');
		var ctoCity_now = $("#ctoCity").val();
	    if(ctoCity!= "" && ctoCity!=null && ctoCity!="null" && ctoCity_now=="中文/拼音"){
			$("#ctoCity").val(ctoCity);
			$("#toCity").val(toCity);
		}else{
			if ($("#ctoCity").val() == ""||$("#ctoCity").val() == "null"||$("#ctoCity").val() == null) {

				$("#ctoCity").val("\u4e0a\u6d77");
				$("#toCity").val("SHA");
			}
		}
		var coutTicketCity = $.cookie('coutTicketCity');
		var outTicketCity = $.cookie('outTicketCity');
		var coutTicketCity_now = $("#coutTicketCity").val();

		if(coutTicketCity!= "" && coutTicketCity!=null && coutTicketCity!="null" && coutTicketCity_now=="中文/拼音"){
			$("#coutTicketCity").val(coutTicketCity);
			$("#outTicketCity").val(outTicketCity);
		}else{
			if ($("#coutTicketCity").val() == ""||$("#coutTicketCity").val() == "null"||$("#coutTicketCity").val() == null) {

				$("#coutTicketCity").val("\u5317\u4eac");
				$("#outTicketCity").val("PEK");
			}
		}

}

function setCityDefaultValueContinue(){

		var cfromcity3 = $.cookie('cfromcity3');
		var fromcity3 = $.cookie('fromcity3');
		if(cfromcity3!= "" && cfromcity3!=null&& cfromcity3!="null"){
				$("#cfromCity3").val(cfromcity3);
				$("#fromCity3").val(fromcity3);
			}else{
		if ($("#cfromCity3").val() == ""||$("#cfromCity3").val() == "null"||$("#cfromCity3").val() == null) {

				$("#cfromCity3").val("\u5317\u4eac");
				$("#fromCity3").val("PEK");
			}
		}

		var ctoCity3 = $.cookie('ctocity3');
		var toCity3 = $.cookie('tocity3');
		if(ctoCity3!= "" && ctoCity3!=null&& ctoCity3!="null"){
			$("#ctoCity3").val(ctoCity3);
			$("#toCity3").val(toCity3);
		}else{
		if ($("#ctoCity3").val() == ""||$("#ctoCity3").val() == "null"||$("#ctoCity3").val() == null) {

			$("#ctoCity3").val("\u4e0a\u6d77");
			$("#toCity3").val("SHA");
		}
		}
}

//航程类型
$(function () {
	$("#single_way").click(function () {
		//$("#search_ss2").css({"display":"none"});
		$("#todate").attr("disabled","disabled");
		$("#todate").val("YYYY-MM-DD");
		$("#voyageTypevalue").val(1);
	});
	$("#goback_way").click(function () {
		//$("#search_ss2").css({"display":"block"});
		$("#todate").attr("disabled","");
		$("#todate").val(nextDayToStr(TomorrowToStr()));
		$("#voyageTypevalue").val(2);
		if($("#todate").val()=="")
			$("#todate").val(nextDayToStr($("#fromdate").val()));
		else if(CompareDate($("#fromdate").val(),$("#todate").val())==1){
			$("#todate").val(nextDayToStr($("#fromdate").val()));
		}
	});
	$("#continue_way").click(function () {
		$(".search_ss2").css({"display":"none"});
		$(".search_ss3").css({"display":"block"});
		$("#cfromCity3").suggest(fcitys, {hot_list:fcommoncitys, dataContainer:"#fromCity3", attachObject:"#suggest2"});
		$("#ctoCity3").suggest(fcitys, {hot_list:fcommoncitys, dataContainer:"#toCity3", attachObject:"#suggest4"});
		//setCityDefaultValueContinue();
		if ($("#fromdate3").val() == ""||$("#fromdate3").val() == "null") {
			$("#fromdate3").val(nextDayToStr($("#fromdate").val()));
		}
		$("#voyageTypevalue").val(3);
	});
});
function SetCalendarOptions(e, element, language) {
	e.cancelBubble = true;
	new CalendarWindow({eventElement:element, language:language, onSelected:function (date) {
		$(element).attr("value", date);
	}.bind(this)});
}
//获取最低价
var lowpricegeting=false;
/*function lowprice(){
			if(!lowpricegeting)
				lowpricegeting=true;
			else
				return;

			var val1=document.getElementById('fromCity').value;
			var val2=document.getElementById('toCity').value;
			var val3=document.getElementById('fromdate').value;
			var val4=document.getElementById('gobackFlag').value;
			var val5=document.getElementById('fromdate3').value;
			var val6=document.getElementById('todate').value;
			var val7=document.getElementById('voyageTypevalue').value;
			var val31=document.getElementById('fromCity3').value;
			var val32=document.getElementById('toCity3').value;
			//alert(val4);
			//alert(val7);
			if(val4=="back"){
			//alert(val4);
				if(val7==2)
				{ 	//alert(val7);
					val1=document.getElementById('toCity').value;
					val2=document.getElementById('fromCity').value;
					val3=val6;
				}
				 else if(val7==3){
				 	//alert(val7);
					 val1=val31;
					 val2=val32;
					 val3=val5;
				 }
			}
			if(val1!=null&&val3!=null){

			$.ajax({
			url : "/flight/getlowprice.do",
			type : "post",
			data : {'fromCitylp' : val1 ,'toCitylp' : val2 ,'fromDatelp' : val3},  // 参数
			cache : true,
			dataType : 'json',
			error: function(){alert('航线最低价数据错误');},
			success: lowpriceback
			});
			}else{
			lowpricegeting=false;
			}
			
}*/
//最低价返回处理
/*function lowpriceback(data){
	for(var i=0; i<data.length;i++){
		var low = data[i];
		$("#lowprice"+i).html(low.price);
	}
	lowpricegeting=false;
}*/
//最低价日期改变处理
/*function SevnDayChange(day) {

	var flag = document.getElementById("gobackFlag").value;
	if (flag == "go") {
		document.getElementById("fromdate").value = day;
		document.form0.action = "/flight/flightQuery.do";
	} else {
		if (flag == "back") {
			document.getElementById("gobackFlag").value = "go";
			document.getElementById("fromdate3").value = day;
			document.form0.action = "/flight/flightQuery.do";
		} else {
			document.getElementById("gobackFlag").value = "go";
			document.getElementById("fromdate").value = day;
			document.form0.action = "/flight/flightQuery.do";
		}
	}
	window.location.href = "/flight/flightQuery.do?fromdate=" + day;
}*/
/*function preOrderold(goFlightKey) {

			//alert(goFlightKey);
	var flag = document.getElementById("gobackFlag").value;
	if (flag == "go") {
		document.getElementById("goFlightKey").value = goFlightKey;

				//alert(document.getElementById('goFlightKey').value);
	} else {
		if (flag == "back") {
			document.getElementById("backFlightKey").value = goFlightKey;

				//alert(document.getElementById('backFlightKey').value);
		} else {
			document.getElementById("gobackFlag").value = "go";
			document.getElementById("goFlightKey").value = goFlightKey;

				//alert(document.getElementById('goFlightKey').value);
		}
	}
	document.form0.action = "/flight/flightQuery.do";
	document.form0.submit();
}*/
function preOrderwithcabin(goFlightKey, cabin) {
	//document.getElementById("cabinUsed").value = cabin;
	preOrder(goFlightKey);
}
function preOrder(goFlightKey) {

			//alert(goFlightKey);
	var flag = document.getElementById("gobackFlag").value;
	if (flag == "go") {
		document.getElementById("goFlightKey").value = goFlightKey;

				//alert(document.getElementById('goFlightKey').value);
	} else {
		if (flag == "back") {
			document.getElementById("backFlightKey").value = goFlightKey;

				//alert(document.getElementById('backFlightKey').value);
		} else {
			document.getElementById("gobackFlag").value = "go";
			document.getElementById("goFlightKey").value = goFlightKey;
				//alert(document.getElementById('goFlightKey').value);
		}
	}
	pat(goFlightKey);
}/*
function filterdate(value) {
	if (value == 1) {
		document.getElementById("startingHour").value = "00:00";
		document.getElementById("closingHour").value = "12:00";
	} else {
		if (value == 2) {
			document.getElementById("startingHour").value = "12:00";
			document.getElementById("closingHour").value = "18:00";
		} else {
			if (value == 3) {
				document.getElementById("startingHour").value = "18:00";
				document.getElementById("closingHour").value = "24:00";
			} else {
				document.getElementById("startingHour").value = "";
				document.getElementById("closingHour").value = "";
			}
		}
	}
	document.getElementById("sortFlag").value = "";
	document.form0.action = "/flight/flightQuery.do";
	document.form0.submit();
}*//*
function filterairway(value) {
	document.getElementById("airCompany").value = value;
	document.form0.action = "/flight/flightQuery.do";
	document.form0.submit();
}*//*
function filterairport(value) {
	document.getElementById("toAirport").value = value;
	document.form0.action = "/flight/flightQuery.do";
	document.form0.submit();
}*/
function show(id) {
	var sss = document.getElementById(id);
	if (sss.style.display == "") {
		sss.style.display = "none";
	} else {
		sss.style.display = "";
	}
}/*
function cabinchange(cabinused) {
	document.getElementById("cabinUsed").value = cabinused;
	document.form0.action = "/flight/flightQuery.do";
	document.form0.submit();
}*//*
function sort(sortFlag) {
	document.getElementById("sortFlag").value = sortFlag;
	document.form0.action = "/flight/sortFlight.do";
	document.form0.submit();
}*/
function querySubmit0() {

        	//document.getElementById('sortFlag').value='';

        	//document.getElementById('startingHour').value='';

        	//document.getElementById('closingHour').value='';

        	//document.getElementById('goFlightKey').value='';

        	//document.getElementById('backFlightKey').value='';

        	//document.getElementById('gobackFlag').value='';

        	//document.getElementById('airCompany').value='';
	querySubmit();
}
function querySubmit() {
	if (CheckSearchFormNew()) {
		//saveCitytoCookies();
		$("#searchlock").html("<p>正在查询中.....</p>");
		$("#searchlock").show();
		document.form0.submit();
	}
}
function unlocksearch() {
	$("#searchlock").hide();
}
function getlocktext() {
	var voytype = $("#voyageTypevalue").val();
	var text = "";
	if (voytype == 1) {
		text = "<p>" + $("#cfromCity").val() + "-" + $("#ctoCity").val() + "</p>";
	} else {
		if (voytype == 2) {
			text = "<p>" + $("#cfromCity").val() + "<=>" + $("#ctoCity").val() + "</p>";
		} else {
			if (voytype == 3) {
				text = "<p>" + $("#cfromCity").val() + "-" + $("#ctoCity").val() + $("#cfromCity3").val() + "-" + $("#ctoCity3").val() + "</p>";
			}
		}
	}
	return text;
}

function setInputValue(cityname,inputId){
	var code = GetCityCodeByName(cityname);
	if(!(code==null||code=='')){
		//$("#"+inputId.substring(1)).val(code);
		document.getElementById(inputId.substring(1)).value=code;
	
	if(inputId=='cfromCity'){
		//$("#coutTicketCity").val(cityname);
		//$("#outTicketCity").val(code);
		document.getElementById("coutTicketCity").value=cityname;
		document.getElementById("outTicketCity").value=code;
	}
	}else{
		$("#"+inputId).after(errMsg("请输入有机场的城市名。"));
	}
}
//dataIdType: 0-出发城市，1-到达城市
function GetCityCodeByName(cityname) {
    for (var i = 0; i < fcitys.length; i++) {
        if (fcitys[i][0].toLowerCase() == cityname.toLowerCase() || fcitys[i][1] == cityname || fcitys[i][3].toLowerCase() == cityname.toLowerCase()|| fcitys[i][2].toLowerCase() == cityname.toLowerCase()) 
            return fcitys[i][0];
    }
    return null;
}

/***
$(function () {
$("#cfromCity").blur(function(){
	

	    if ( $(this).val()=='')
		    $(this).after(errMsg(FLIGHT_AJAX_NODEPARTURECITY));
		else {
		 	$(this).next().remove();
		    $("#ctoCity").click();
		}
	});
	$("#ctoCity").blur(function(){
	    if ($(this).val()=='')
		    $(this).after(errMsg(FLIGHT_AJAX_NODESTCITY));
		else {
		 $(this).next().remove();
		    $("#fromdate").focus();
		}
	});
		$("#fromdate").blur(function(){
	    if ($(this).val()=='')
		    $(this).after(errMsg(FLIGHT_AJAX_NODESTCITY));
		else {
		 $(this).next().remove();
		 var fly_type = document.getElementById("voyageTypevalue").value;

		 if(fly_type=='2'){
		    $("#todate").focus();
		    }
		 else if(fly_type=='3'){
		     $("#cfromCity3").click();
		    }
		}
	});
	$("#cfromCity3").blur(function(){
	    if ( $(this).val()=='')
		    $(this).after(errMsg(FLIGHT_AJAX_NODEPARTURECITY));
		else {
		    $(this).next().remove();
		    $("#ctoCity3").click();
		}
	});
	$("#ctoCity3").blur(function(){
	    if ( $(this).val()=='')
		    $(this).after(errMsg(FLIGHT_AJAX_NODESTCITY));
		else {
		    $(this).next().remove();
		    $("#fromdate3").focus();
		}
	});
	$("#fromdate3").blur(function(){
	    if ( $(this).val()=='')
		    $(this).after(errMsg(FLIGHT_RETURNDATE_NULL));
		else {
		    $(this).next().remove();
		}
	});
	$("#coutTicketCity").blur(function(){
	    if ($(this).val()=='')
		    $(this).after(errMsg(FLIGHT_AJAX_INVALIDOUTCITY));
		else {
		    $(this).next().remove();
		}
	});
});
**/